package com.yuwj.chinaa.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityBurseCoinNumber;
import com.yuwj.chinaa.entity.EntityExercise;
import com.yuwj.chinaa.entity.EntityPartnerApply;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加行权界面
 */
public class ExerciseAddActivity extends BaseActivity {
    IPresenter presenter;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_total_assets)
    TextView tvTotalAssets;
    @BindView(R.id.tv_had_exercise)
    TextView tvHadExercise;
    @BindView(R.id.tv_no_exercise)
    TextView tvNoExercise;
    @BindView(R.id.et_apply_exercise)
    EditText etApplyExercise;
    private EntityExercise entityExercise;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_exercise_add;
    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, ExerciseAddActivity.class);
        return intent;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);

    }

    @Override
    public void initView() {
        tvTitle.setText("我的行权");
        if (entityExercise!=null){
            tvTotalAssets.setText("总资产："+entityExercise.getCoinNumber());
            tvHadExercise.setText("已行权个数：️"+entityExercise.getAlreadyExerciseNumber());
            tvNoExercise.setText("未行权个数：️"+entityExercise.getNotExerciseNumber());
        }


    }

    @Override
    public void initOperator() {
        presenter.getExerciseOrder("");

    }

    @OnClick({R.id.iv_back, R.id.rl_add_exercise})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.rl_add_exercise:
                JSONObject param=new JSONObject();
                String str=etApplyExercise.getText().toString();
                int applyCount=0;
                try {
                     applyCount=Integer.parseInt(str);

                }catch (Exception e){
                    ToastUtil.getInstance().shortLength(context,"请输入正整数册数");
                    return;
                }
                try {
                    param.put("exerciseNumber",applyCount+"");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.addExerciseOrder(param.toString());
                break;
        }
    }

    @Override
    public void onAddExerciseOrderSuccess(String resp) {
        super.onAddExerciseOrderSuccess(resp);
        ToastUtil.getInstance().shortLength(context,"申请行权成功");
        finish();
    }

    @Override
    public void onAddExerciseOrderFail(Exception e) {
        super.onAddExerciseOrderFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage() + "");
    }

    @Override
    public void onGetExerciseOrderSuccess(EntityExercise entityExercise) {
        super.onGetExerciseOrderSuccess(entityExercise);
        this.entityExercise=entityExercise;
        initView();
    }

    @Override
    public void onGetExerciseOrderFail(Exception e) {
        super.onGetExerciseOrderFail(e);
        ToastUtil.getInstance().shortLength(context,e.getMessage());
    }
}
